package com.yantong.service.impl;

import com.yantong.mapper.CentreMapper;
import com.yantong.pojo.Centre;
import com.yantong.exception.BDException;
import com.yantong.utils.PageUtils;
import com.yantong.pojo.PayChannel;
import com.yantong.mapper.PayChannelMapper;
import com.yantong.service.PayChannelService;
import com.yantong.utils.ValidateUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 支付通道服务
 *
 * @author kanghaixin
 * @date 2018/10/21.16:53
 */
@Service
public class PayChannelServiceImpl implements PayChannelService {
    @Autowired
    private PayChannelMapper payChannelMapper;
    @Autowired
    private CentreMapper centreMapper;


    @Override
    public PageUtils<PayChannel> listChannels(Long centerId, String name, int offset, int limit) {
        Map<String, Object> params = new HashMap<>(2);
        if (StringUtils.isNotEmpty(name)) {
            params.put("name", name);
        }
        if (null != centerId) {
            params.put("centerId", centerId);
        }

        PageUtils<PayChannel> pageUtils = new PageUtils<>();
        int total = payChannelMapper.countByParams(params);
        if (total > 0) {
            pageUtils.setTotal(total);
            pageUtils.setRows(payChannelMapper.listByParams(params, new RowBounds(offset, limit)));
        }
        return pageUtils;
    }

    @Override
    public int savePayChannel(PayChannel payChannel) {
        if (payChannel == null) {
            return 0;
        }
        //表示启用，则需要将其他的设置为禁用
        if (payChannel.getDel() != null && payChannel.getDel() == 0) {
            //先把商户的通道都设置为禁用
            PayChannel pc = new PayChannel();
            pc.setCenterId(payChannel.getCenterId());
            pc.setOperId(payChannel.getOperId());
            pc.setOperName(payChannel.getOperName());
            pc.setDel(1);
            payChannelMapper.updateByPrimaryKeySelective(pc);
        }
        return payChannelMapper.insertSelective(payChannel);
    }

    @Override
    public PayChannel getByKey(Long id) {
        if (id == null) {
            return null;
        }
        return payChannelMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updatePayChannel(PayChannel payChannel) {
        if (payChannel == null || payChannel.getId() == null) {
            return 0;
        }
        PayChannel currentPayChannel = payChannelMapper.selectByPrimaryKey(payChannel.getId());
        if (null == currentPayChannel) {
            throw new BDException("支付通道异常，请重新选择后操作");
        }
        return payChannelMapper.updateByPrimaryKeySelective(payChannel);
    }

    @Override
    public int removePayChannel(PayChannel payChannel) {
        if (payChannel == null || payChannel.getId() == null) {
            return 0;
        }
        PayChannel currentPayChannel = payChannelMapper.selectByPrimaryKey(payChannel.getId());
        if (null == currentPayChannel) {
            throw new BDException("支付通道异常，请重新选择后操作");
        }
        if (currentPayChannel.getDel() == 0) {
            throw new BDException("启用状态的支付通道无法删除，请重新选择后操作");
        }
        return payChannelMapper.deleteByPrimaryKey(payChannel.getId());
    }

    @Override
    public int disableShow(Long channelId, Long aid, String aname) {
        if (channelId == null) {
            return 0;
        }
        PayChannel pc = new PayChannel();
        pc.setId(channelId);
        pc.setOperId(aid);
        pc.setOperName(aname);
        pc.setDisplay(0);
        return payChannelMapper.updateByPrimaryKeySelective(pc);
    }

    @Override
    public int enableShow(Long channelId, Long aid, String aname) {
        if (channelId == null) {
            return 0;
        }
        PayChannel pc = new PayChannel();
        pc.setId(channelId);
        pc.setOperId(aid);
        pc.setOperName(aname);
        pc.setDisplay(1);
        return payChannelMapper.updateByPrimaryKeySelective(pc);
    }

//    @Override
//    public List<PayChannel> listAllChannels() {
//        return payChannelMapper.listAllChannels();
//    }

    @Override
    public int enableUse(Long centerId, Long channelId, Long aid, String aname) {
        if (channelId == null || centerId == null) {
            return 0;
        }

        //先把商户的通道都设置为禁用
        PayChannel pc = new PayChannel();
        pc.setCenterId(centerId);
        pc.setOperId(aid);
        pc.setOperName(aname);
        pc.setDel(1);
        payChannelMapper.updateByCentreIdSelective(pc);

        //将商户选中的通道设置为启用
        pc.setDel(0);
        pc.setId(channelId);
        return payChannelMapper.updateByPrimaryKeySelective(pc);
    }

    @Override
    public PayChannel getFirstChannel(Long centreId) {
        PayChannel payChannel = null;
        if (null != centreId) {//商户ID存在，直接取商户的支付渠道
            payChannel = payChannelMapper.getFirstChannel(centreId);
        }
        if (null == payChannel) {//商户对应的渠道不存在，直接取平台的渠道
            payChannel = payChannelMapper.getPlatformFirstChannel();
        }
        return payChannel;
    }

    @Override
    public void copyFromPlatform(Long centerId) {
        List<PayChannel> platformPayChannerls = payChannelMapper.getPlatformChannels();
        if (ValidateUtil.isNotEmptyCollection(platformPayChannerls)) {
            Centre centre = centreMapper.selectByPrimaryKey(centerId);
            for (PayChannel payChannel : platformPayChannerls) {
                PayChannel currentChannel = payChannelMapper.getChannelByCodeAndCenterId(payChannel.getChannelCode(), centerId);
                if (null == currentChannel && payChannel.getDisplay() == 1) {
                    payChannel.setId(null);
                    payChannel.setCenterId(centerId);
                    payChannel.setCenterName(centre.getName());
                    payChannel.setCreateTime(new Date());
                    payChannelMapper.insertSelective(payChannel);
                }
            }
        }
    }
}
